package com.ruoyi.system.interAttr.attr;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.UUIDGenerator;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.V2.gameAttr.mapper.GameAttributeMapper;
import com.ruoyi.system.interAttr.domain.GamesRes;
import com.ruoyi.system.interAttr.domain.InterAttrRes;
import com.ruoyi.system.interAttr.domain.InterServerRes;
import com.ruoyi.system.model.domain.SalePartner;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Slf4j
@Service
public class MKeJinShouAttr {
    @Resource
    GameAttributeMapper gameAttributeMapper;
    @Resource
    private RedisCache redisCache;


    private static String SERVICE_NAME = "KJS";
    private static String getCookie(SalePartner salePartner){

//        String s = new Date().getTime() / 1000 + "";

        String cookie = "account=" + salePartner.getSaleKey() +"; _ga=GA1.1.2133416559.1726621808; acw_tc=ac11000117266272807932614e00e00608a859cf72b9d0f1ed3cb79246450c; _ga_TJJQCLLECR=GS1.1.1726627284.2.0.1726627284.0.0.0";
        return cookie;
    }


    public static void main(String[] args) {
        String url = "https://kejinlianmeng.com/account/goods/establish?game_id="+1+"&back_url=https%3A%2F%2Fkejinlianmeng.com%2Faccount%2Fgoods%2Findex%3F_pjax%3D%2523main%26game_id%3D1&_pjax=%23main";
        String response = HttpRequest.get(url).header("Cookie","_ga=GA1.1.1278412161.1681543055; account=Apxo85fHzxZGGehuyRD6bBI2N4yDODG754AmLFir; remember_web_59ba36addc2b2f9401580f014c7f58ea4e30989d=eyJpdiI6IjlPUVRNTHhIVms1OXJqdzNcL2NpRlFnPT0iLCJ2YWx1ZSI6InFcL3daM2UyTk9wbWNyRG13RmJzT0lZVTRhMnVVWE04SXVFTVwvRHU4ZEE1TkZTbXdyXC8yNm5XZ3owam1QZW9Yd3NzKzBnbW5xcTQwOWRpVmpLWUYwNVpzSFpMR2ZPcTRWMGp3dHZTSldzbmVDZENSbU5cL3praklvSUVkRms2ZDEyWUFFenFxTkJiSVwvc2xlNzRYeGtJNW83SzZhUVREMU1zeWlodmJlXC9hcGtIVFJMSFU4RWx5ZDJna1RWV2l5MHVjOSIsIm1hYyI6ImRkODI0OWYwODk5NTZlMWEwNTM4NTE4NjZjZjBlYjAyOTU2OTkyMzM4YzA1YjA3MWJmNTM3ODI5M2E4MGRlOTQifQ%3D%3D; _ga_TJJQCLLECR=GS1.1.1730685924.35.0.1730685924.0.0.0").execute().body();
        Document document = Jsoup.parse(response);
//        Elements elements = document.getElementsByAttributeValue("data-type","radio");
        Elements elements = document.getElementsByClass("layui-form-item");
        for(Element element : elements){
            Elements divs = element.select("div");
            for(Element div : divs) {
                if(div.attr("data-type").equals("radio")){
                    Element element1 = divs.get(0);
                    Elements element2 = element1.getElementsByClass("layui-input-block");
                    System.out.println(element1);
                    System.out.println(element2.attr("id"));
                }
            }
        }
    }

    public List<GamesRes> getGames(SalePartner salePartner) {
        String url = "https://kejinlianmeng.com/api_up/account/config/games";
        Map params = getCove();
        String paramsStr = JSONObject.toJSONString(params);
        String response = HttpUtils.sendPost(url, paramsStr, "application/json");
//        System.out.println(response);
        JSONObject resJson = JSONObject.parseObject(response);
        List<JSONObject> jsonObjects = JSONObject.parseArray(resJson.getString("data"), JSONObject.class);

        List<GamesRes> gamesResList = new ArrayList<>();

        for (JSONObject jsonObject : jsonObjects) {
            GamesRes gamesRes = new GamesRes();
            String id = jsonObject.getString("id");
            String title = jsonObject.getString("title");
            gamesRes.setId(id);
            gamesRes.setGameName(title);
            gamesResList.add(gamesRes);
        }
        return gamesResList;
    }

    public  List<InterServerRes> getServer(String gameId, SalePartner salePartner) {
        List<InterServerRes> interServerRes = new ArrayList<>();
        if(gameId.equals("47")){
            InterServerRes interServer = new InterServerRes();
            interServer.setId("永劫无间不匹配区服跳过即可");
            interServer.setName("永劫无间不匹配区服跳过即可");
            interServerRes.add(interServer);
        }else {
            String url = "https://kejinlianmeng.com/account/goods/establish?game_id=" + gameId + "&back_url=https%3A%2F%2Fkejinlianmeng.com%2Faccount%2Fgoods%2Findex%3F_pjax%3D%2523main%26game_id%3D1&_pjax=%23main";
            String response = HttpRequest.get(url).header("Cookie", getCookie(salePartner)).execute().body();
            Document document = Jsoup.parse(response);
            Elements elements = document.getElementsByAttributeValue("lay-filter", "area");
            Elements option = elements.select("option");
            List<Element> elements1 = option.subList(1, option.size());
//        InterServerRes interServer = new InterServerRes();
//        interServer.setId(name);
//        interServer.setName(text);
//        interServer.setUuid(UUIDGenerator.generateUuid6());
//        List<InterServerRes> interServerRes1 = new ArrayList<>();

            for (Element element : elements1) {
                InterServerRes interServer = new InterServerRes();
                interServer.setId(element.attr("value"));
                interServer.setName(element.text());
                interServer.setUuid(UUIDGenerator.generateUuid6());

                //获取服务器
                String server = getServer(gameId, element.attr("value"),salePartner);
                Document documents = Jsoup.parse(server);
                Elements options = documents.select("option");
                List<Element> elements2 = options.subList(1, options.size());
                List<InterServerRes> interServerRes2 = new ArrayList<>();
                for (Element element1 : elements2) {
                    InterServerRes interServer2 = new InterServerRes();
                    String name = element1.text();
                    String value = element1.attr("value");
                    interServer2.setId(value);
                    interServer2.setName(name);
                    interServer2.setUuid(UUIDGenerator.generateUuid6());
                    interServerRes2.add(interServer2);
                }
                interServer.setInterServerRes(interServerRes2);
                interServerRes.add(interServer);
            }
        }
        return interServerRes;
    }

    String getServer(String gameId,String areaId,SalePartner salePartner){
        String url = "https://kejinlianmeng.com/account/public/servers?game_id="+gameId+"&area_id="+areaId+"&force_server=1&open=1";
        String response = HttpRequest.get(url).header("Cookie",getCookie(salePartner)).execute().body();

        return response;
    }

    public List<InterAttrRes> getPropety(String gameId, String type, SalePartner salePartner) {

        String url = "https://kejinlianmeng.com/account/goods/establish?game_id="+gameId+"&back_url=https%3A%2F%2Fkejinlianmeng.com%2Faccount%2Fgoods%2Findex%3F_pjax%3D%2523main%26game_id%3D1&_pjax=%23main";
        String response = HttpRequest.get(url).header("Cookie",getCookie(salePartner)).execute().body();
        Document document = Jsoup.parse(response);
//        Elements elements = document.getElementsByAttributeValue("data-type","radio");
        Elements elements = document.getElementsByClass("layui-form-item");
        List<InterAttrRes> interAttrResList = new ArrayList<>();
        for(Element element : elements){
            Elements divs = element.select("div");
            for(Element div : divs) {
                Element element1 = divs.get(0);
                if (Objects.equals("1", type)) {
                    if (div.attr("data-type").equals("radio")) {
                        InterAttrRes interAttrRes = new InterAttrRes();
                        Elements elementsByClass = element1.getElementsByClass("layui-input-block");
                        String id = elementsByClass.attr("id");
                        String title = element1.text();
                        Elements inputs = element1.select("input");
                        interAttrRes.setId(id);
                        interAttrRes.setName(title);
                        interAttrRes.setUuid(UUIDGenerator.generateUuid6());
                        List<InterAttrRes> interAttrResList1 = new ArrayList<>();
                        for (Element input : inputs) {
                            InterAttrRes interAttrRes1 = new InterAttrRes();
                            String id1 = input.attr("title");
                            String title1 = input.attr("value");
                            interAttrRes1.setName(id1);
                            interAttrRes1.setId(title1);
                            interAttrRes1.setUuid(UUIDGenerator.generateUuid6());
                            interAttrResList1.add(interAttrRes1);
                        }
                        interAttrRes.setInterServerRes(interAttrResList1);
                        interAttrResList.add(interAttrRes);
                    }
                } else if (Objects.equals("2", type)) {
                    if (div.attr("data-type").equals("checkbox")) {
                        InterAttrRes interAttrRes = new InterAttrRes();
                        Elements elementsByClass = element1.getElementsByClass("layui-input-block");
                        String id = elementsByClass.attr("id");
                        String title = element1.text();
                        Elements inputs = element1.select("input");
                        interAttrRes.setId(id);
                        interAttrRes.setName(title);
                        interAttrRes.setUuid(UUIDGenerator.generateUuid6());
                        List<InterAttrRes> interAttrResList1 = new ArrayList<>();
                        for (Element input : inputs) {
                            String value = input.attr("value");
                            if (!value.equals("1")) {
                                InterAttrRes interAttrRes1 = new InterAttrRes();
                                String name = input.attr("title");
                                interAttrRes1.setId(value);
                                interAttrRes1.setName(name);
                                interAttrRes1.setUuid(UUIDGenerator.generateUuid6());
                                interAttrResList1.add(interAttrRes1);
                            }
                        }
                        interAttrRes.setInterServerRes(interAttrResList1);
                        interAttrResList.add(interAttrRes);
                    }
                } else if (Objects.equals("3", type)) {
                    if (div.attr("data-type").equals("input")) {
                        InterAttrRes interAttrRes = new InterAttrRes();
                        Elements elementsByClass = element1.getElementsByClass("layui-input-block");
                        String id = elementsByClass.attr("id");
                        String title = element1.text();
                        interAttrRes.setId(id);
                        interAttrRes.setName(title);
                        interAttrRes.setUuid(UUIDGenerator.generateUuid6());
                        interAttrResList.add(interAttrRes);
                    }
                }
            }
        }
        return interAttrResList;
    }

    static Map getCove() {
        String key = "878kjs36bfd324d8";
        String secret = "dd989607c0c5b35422a4f69d306bb0";

        JSONObject params = new JSONObject();
        params.put("key", key);
        params.put("secret", secret);
        String uuid4 = UUIDGenerator.generateUuid6();
        params.put("rand_str", uuid4);
        long time = DateUtils.getNowDate().getTime() / 1000;
        params.put("timestamp", time);
        String oldSign = "key=" + key + ",secret=" + secret + ",rand_str=" + uuid4 + ",timestamp=" + time;
        String sign = DigestUtils.md5Hex(oldSign);
        sign = DigestUtils.sha1Hex(sign + uuid4);
        params.put("sign", sign);
        return params;
    }


}